package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox chexkbox = findViewById(R.id.chexkbox);
        EditText EditText = findViewById(R.id.editTextText);
        EditText EditText2 = findViewById(R.id.editTextNumberPassword);

        // 判断用户是否登录过
        String[] loginDatas = new util().getLoginData(this);
        EditText.setText(loginDatas[0]);
        EditText2.setText(loginDatas[1]);

        Gson gson = new Gson();
        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                Log.i("tag", "111111111");
                String response = msg.obj.toString();
                Denglu denglu = gson.fromJson(response, Denglu.class);
                if (denglu.getCode() == 200) {
                    // 判断是否勾选记住密码
                    if (chexkbox.isChecked()){
                        new util().saveLoginData(MainActivity.this,EditText.getText().toString(),EditText2.getText().toString());
                    }
                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                    startActivity(intent);
                }
//                return false;
            }
        };

        Button button = findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                CheckBox chexkbox = findViewById(R.id.chexkbox);
                boolean isCgecked = chexkbox.isChecked();
//                EditText EditText = findViewById(R.id.editTextText);
                String sf = EditText.getText().toString().trim();
//                EditText EditText2 = findViewById(R.id.editTextNumberPassword);
                String wsdac = EditText2.getText().toString().trim();

                if (sf.matches("^[a-zA-Z0-9]{1,11}$") && isCgecked && wsdac.matches("^[a-zA-Z0-9]{1,11}$")) {
                    new util().POST("https://photo.vshare.site/online/test/login", "", "{\"username\":" + sf + ",\"password\":" + wsdac + "}", handler);

                } else {
                    Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
                }
            }

        });


        Button button4 = findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity4.class);
                startActivityForResult(intent,100);
            }
        });

//        Intent intent = getIntent();
//        EditText editTextText = findViewById(R.id.editTextText);
//        EditText editTextNumberPassword = findViewById(R.id.editTextNumberPassword);
//        String errt = intent.getStringExtra("ingput");
//        String errt2 = intent.getStringExtra("ingput1");
//        editTextText.setText(errt);
//        editTextNumberPassword.setText(errt2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 101){
            EditText editTextText = findViewById(R.id.editTextText);
            EditText editTextNumberPassword = findViewById(R.id.editTextNumberPassword);
            String errt = data.getStringExtra("ingput");
            String errt2 = data.getStringExtra("ingput1");
            editTextText.setText(errt);
            editTextNumberPassword.setText(errt2);
        }
    }

    //        public void POST(String url){
//            OkHttpClient client = new OkHttpClient();
//            Request request = new Request.Builder().url(url).build();
//
//        }
}